<?php

namespace app\admin\controller\dormitory;

use app\admin\controller\auth\Department;
use app\admin\model\DormitoryBuilding;
use app\admin\model\DormitoryDormBed;
use app\admin\model\DormitoryGarden;
use app\admin\model\DormitoryDorm;
use app\common\controller\Backend;
use think\Db;

class Bed extends Backend
{
    /**
     * @var \app\admin\model\DormitoryDormBed
     */
    protected $model = null;
    protected $searchFields = 'number';
    protected $noNeedRight = ['cxSelect', 'bedList'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('DormitoryDormBed');

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->alias('a')
                ->join('__DORMITORY_DORM__ b', 'b.number = a.dorm_number and b.garden_number = a.garden_number and b.building_number = a.building_number', 'left')
                ->join('__DORMITORY_GARDEN__ c', 'c.number = a.garden_number', 'left')
                ->join('__DORMITORY_BUILDING__ d', 'd.number = a.building_number and d.garden_number = a.garden_number', 'left')
                ->join('__STUDENT__ e', 'e.id = a.student_id', 'left')
                ->join('__BASE_FACULTY__ f', 'f.id = e.faculty_id', 'left')
                ->where($where)
                ->where('b.status', DormitoryDorm::NORMAL)
                ->count();

            $list = $this->model
                ->alias('a')
                ->join('__DORMITORY_DORM__ b', 'b.number = a.dorm_number and b.garden_number = a.garden_number and b.building_number = a.building_number', 'left')
                ->join('__DORMITORY_GARDEN__ c', 'c.number = a.garden_number', 'left')
                ->join('__DORMITORY_BUILDING__ d', 'd.number = a.building_number and d.garden_number = a.garden_number', 'left')
                ->join('__STUDENT__ e', 'e.id = a.student_id', 'left')
                ->join('__BASE_FACULTY__ f', 'f.id = e.faculty_id', 'left')
                ->where($where)
                ->where('b.status', DormitoryDorm::NORMAL)
                ->field('a.*,b.name as dorm_name,b.floor as dorm_floor,c.name as garden_name,d.name as building_name,e.name as student_name,e.number student_number,e.id_number as student_id_number,f.name as faculty_name')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            if ($list) {
                $list = collection($list)->append(['status_text'])->toArray();
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 床位使用情况
     */
    public function statusList()
    {
        $statusList = DormitoryDormBed::$statusList;

        $data = [];
        foreach ($statusList as $key => $value) {
            $data['searchlist'][] = ['id' => $key, 'name' => $value];
        }

        $this->success('', null, $data);
    }

    /**
     * 联动搜索
     * 根据楼栋号获取寝室列表
     */
    public function cxSelect()
    {
        $garden_number = $this->request->request('garden_number');
        $building_number = $this->request->request('building_number');
        $dorm_number = $this->request->request('dorm_number');
        $status = $this->request->request('status', 'all');

        $where['garden_number'] = $garden_number;
        $where['building_number'] = $building_number;
        $where['dorm_number'] = $dorm_number;
        if ($status != 'all') {
            $where['status'] = $status;
        }

        $result = $this->model
            ->where($where)
            ->select();

        $searchList = [];
        foreach ($result as $key => $value) {
            $searchList[] = ['value' => $value['id'], 'name' => $value['number'], 'id' => $value['id']];
        }

        $this->success('', null, $searchList);
    }

    public function bedList()
    {
        $garden_number = $this->request->request('garden_number');
        $building_number = $this->request->request('building_number');
        $dorm_number = $this->request->request('dorm_number/a');
        $status = $this->request->request('status', 'all');

        $where['garden_number'] = $garden_number;
        $where['building_number'] = $building_number;
        $where['dorm_number'] = ['in', $dorm_number];
        if ($status != 'all') {
            $where['status'] = $status;
        }

        $result['bed'] = $this->model
            ->where($where)
            ->field('id,number,garden_number,building_number,dorm_number')
            ->select();

        $this->success('', null, $result);
    }

}
